import QtQuick
import FluentUI
import QtQuick.Layouts
import QtQuick.Controls
import Qt5Compat.GraphicalEffects

import "components/"
import "pages/"

import MyApp

FluWindow {
    id: root
    width: 700
    height: 500
    visible: true
    title: qsTr("测量结果")
    showStayTop: true
    showDark: true
    launchMode: FluWindowType.SingleTask
    Component {
        id: com_page
        ColumnLayout {
            id: com_page_layout
            property var result: argument
            anchors.fill: parent
            Row {
                Layout.topMargin: 10
                Layout.leftMargin: 15
                // Layout.bottomMargin: 5
                FluText {
                    text: {
                        switch (com_page_layout.result.task_type) {
                        case Types.GET_EIS:
                            return "阻抗图谱 - "
                        case Types.GET_SINGLE:
                            return "单点阻抗 - "
                        default:
                            return "未知类型 - "
                        }
                    }
                    font.pixelSize: 20
                    font.bold: true
                }
                FluText {
                    Layout.topMargin: 10
                    Layout.leftMargin: 10
                    Layout.bottomMargin: 5
                    text: com_page_layout.result.create_time
                    font.pixelSize: 20
                    font.bold: true
                }
            }
            FluScrollablePage {
                Layout.leftMargin: 10
                Layout.bottomMargin: 10
                Layout.rightMargin: 10
                Layout.fillHeight: true
                Layout.fillWidth: true
                ColumnLayout {
                    Layout.fillWidth: true
                    // Layout.preferredHeight: 800

                    RowLayout {
                        // anchors.fill: parent
                        Layout.fillWidth: true
                        Layout.preferredHeight: 400
                        FluFrame {
                            Layout.fillWidth: true
                            Layout.preferredHeight: 400
                            ScatterChart {
                                id: nquist_chart
                                anchors.fill: parent
                            }
                        }
                        ColumnLayout {
                            Layout.preferredWidth: 300
                            Layout.fillHeight: true
                            Layout.leftMargin: 10
                            FluText {
                                Layout.topMargin: 5
                                Layout.bottomMargin: 5
                                text: qsTr("详细信息")
                                font.pixelSize: 16
                                font.bold: true
                            }
                            Row {
                                FluText {
                                    text: "测量类型："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    text: {
                                        switch (com_page_layout.result.task_type) {
                                        case Types.GET_EIS:
                                            return "阻抗图谱"
                                        case Types.GET_SINGLE:
                                            return "单点阻抗"
                                        default:
                                            return "未知类型"
                                        }
                                    }
                                    font.pixelSize: 14
                                }
                            }
                            Row {
                                FluText {
                                    text: "测量时间："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    text: com_page_layout.result.create_time.toLocaleString(
                                              Qt.LocalTime,
                                              "yyyy-MM-dd hh:mm:ss")
                                    font.pixelSize: 14
                                }
                            }
                            Row {
                                FluText {
                                    text: "驱动电流："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    text: {
                                        switch (com_page_layout.result.driving_current) {
                                        case Types.AMPL_1A:
                                            return "1A"
                                        case Types.AMPL_A15:
                                            return "0.15A"
                                        case Types.AMPL_A25:
                                            return "0.25A"
                                        case Types.AMPL_A5:
                                            return "0.5A"
                                        case Types.AMPL_A8:
                                            return "0.8A"
                                        default:
                                            return "未知电流"
                                        }
                                    }
                                    font.pixelSize: 14
                                }
                            }
                            Row {
                                FluText {
                                    text: "频点数："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    text: com_page_layout.result.points.length
                                    font.pixelSize: 14
                                }
                            }
                            Row {
                                FluText {
                                    text: "电池电压："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    text: com_page_layout.result.points[0].voltage.toFixed(
                                              2) + " V"
                                    font.pixelSize: 14
                                }
                            }

                            Row {
                                FluText {
                                    text: "欧姆阻抗（两点估计）："
                                    font.pixelSize: 14
                                }
                                FluCopyableText {
                                    id: ohmic_impedance_text
                                    text: "无法计算"
                                    font.pixelSize: 14
                                }
                            }

                            Item {
                                Layout.fillHeight: true
                            }
                        }
                    }
                    FluFrame {
                        Layout.fillWidth: true
                        Layout.preferredHeight: 200
                        ScatterChart {
                            id: af_chart
                            anchors.fill: parent
                            title: "幅频响应图"
                        }
                    }

                    FluFrame {
                        Layout.fillWidth: true
                        Layout.preferredHeight: 200
                        ScatterChart {
                            id: pf_chart
                            anchors.fill: parent
                            title: "相频响应图"
                        }
                    }
                }
            }

            Component.onCompleted: {
                argument.points.forEach(function (point) {
                    nquist_chart.points_data.push({
                                                        "x": point.real * 1000,
                                                        "y": -point.imag * 1000
                                                    })
                    af_chart.points_data.push({
                                                  "x": point.freq,
                                                  "y": Math.sqrt(
                                                           point.real * point.real + point.imag
                                                           * point.imag) * 1000
                                              })
                    pf_chart.points_data.push({
                                                  "x": point.freq,
                                                  "y": Math.atan2(
                                                           point.imag,
                                                           point.real)
                                              })
                })
                if (argument.points.length > 0) {
                    // 计算欧姆阻抗，曲线与 X 轴的交点
                    // 找到第一个imag由正变负的点
                    var firstCrossIndex = -1
                    for (var i = 0; i < argument.points.length; i++) {
                        if (argument.points[i].imag < 0) {
                            firstCrossIndex = i
                            break
                        }
                    }
                    if (firstCrossIndex !== -1) {
                        var point1 = argument.points[firstCrossIndex - 1]
                        var point2 = argument.points[firstCrossIndex]
                        // 线性插值计算交点
                        var slope = (point2.imag - point1.imag) / (point2.real - point1.real)
                        var intercept = point1.imag - slope * point1.real
                        var realPart = -intercept / slope
                        // 交点的实部
                        ohmic_impedance_text.text = (realPart * 1000).toFixed(
                                    3) + " mΩ"
                    }
                }
            }
        }
    }

    FluTabView {
        id: tab_view
        Layout.fillHeight: true
        Layout.fillWidth: true
        addButtonVisibility: false
        Component.onCompleted: {

            // tab_view.appendTab("/EIS_Desktop/res/favicon.ico", qsTr("主页"),
            //                    com_page, {})
        }
    }

    onArgumentChanged: {
        '/;'
        var title = ""
        switch (argument.task_type) {
        case Types.GET_EIS:
            title = "阻抗图谱 - "
            break
        case Types.GET_SINGLE:
            title = "单点阻抗 - "
            break
        default:
            title = "未知类型 - "
            break
        }
        title += argument.create_time.toLocaleString(Qt.LocalTime,
                                                     "yyyy-MM-dd hh:mm:ss")

        tab_view.appendTab("/EIS_Desktop/res/favicon.ico", title, com_page,
                           root.argument)
        // console.log(tab_view.currentIndex)
    }
}
